package com.xjrsoft.module.demo.controller;


import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.entity.XjrBaseUser;
import com.xjrsoft.module.base.entity.XjrBaseUserRelation;
import com.xjrsoft.module.base.service.IXjrBaseUserRelationService;
import com.xjrsoft.module.demo.entity.XjrBaseMaterial;
import com.xjrsoft.module.demo.service.IXjrBaseMaterialService;
import com.xjrsoft.module.demo.vo.ItemVo;
import com.xjrsoft.module.demo.vo.MaterialVo;
import com.xjrsoft.module.login.entity.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 物料表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-04-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("/material")
@Api(value = "/material", tags = "物料模块")
public class XjrBaseMaterialController {

    private final IXjrBaseMaterialService materialService;

    private final IXjrBaseUserRelationService userRelationService;

    @GetMapping
    @ApiOperation(value = "获取物料列表")
    public Response<List<MaterialVo>> getList(){

        SaSession tokenSession = StpUtil.getTokenSession();
        //获取到当前登陆人信息
        XjrBaseUser user = tokenSession.get("user", new XjrBaseUser());


        // 2.0.0.8 以前版本  使用此方法获取当前登陆人信息
        XjrUser user1 = SecureUtil.getUser();


        //获取到当前登陆人部门id
        List<XjrBaseUserRelation> userRelationList = userRelationService.list(Wrappers.lambdaQuery(XjrBaseUserRelation.class)
                .eq(XjrBaseUserRelation::getUserId, user.getUserId()).eq(XjrBaseUserRelation::getCategory, 3));
        List<String> deptIds = userRelationList.stream().map(XjrBaseUserRelation::getObjectId).collect(Collectors.toList());


        //获取到当前部门下的所有用户
        List<XjrBaseUserRelation> allUser = userRelationService.list(Wrappers.lambdaQuery(XjrBaseUserRelation.class)
                .in(XjrBaseUserRelation::getObjectId, deptIds).eq(XjrBaseUserRelation::getCategory, 3));

        //当前部门下所有人员的id
        List<String> userIds = allUser.stream().map(XjrBaseUserRelation::getUserId).collect(Collectors.toList());

        //根据当前部门下所有人id 来过滤数据
        List<XjrBaseMaterial> list = materialService.list(Wrappers.lambdaQuery(XjrBaseMaterial.class)
                .in(XjrBaseMaterial::getCreateUserId,userIds));

        //根据当前部门下所有人id 来过滤数据 （这里是分页的）
        Page<XjrBaseMaterial> page = materialService.page(new Page<>(1, 10), Wrappers.lambdaQuery(XjrBaseMaterial.class)
                .in(XjrBaseMaterial::getCreateUserId, userIds));

        return Response.ok(BeanUtil.copyList(list, MaterialVo.class));
    }
}
